import matplotlib.pyplot as plt

decisionNode = dict(boxstyle="sawtooth", fc="0.8")
leafNode = dict(boxstyle="round4", fc="0.8")
arrow_args = dict(arrowstyle="<-")


def createPlot():
    fig = plt.figure(1, facecolor='white')
    fig.clf()
    axprops = dict(xticks=[], yticks=[])
    createPlot.ax1 = plt.subplot(111, frameon=False, **axprops)  # no ticks
    plotNode('aaaaa', (0.5, 0.1), (0.1, 0.5), decisionNode)
    plotNode('bbbbb', (0.8, 0.1), (0.3, 0.8), decisionNode)
    plt.show()


def plotNode(nodeTxt, centerPt, parentPt, nodeType):
    createPlot.ax1.annotate(nodeTxt, xy=parentPt, xycoords='axes fraction',
                            xytext=centerPt, textcoords='axes fraction',
                            va="center", ha="center", bbox=nodeType, arrowprops=arrow_args)


def getNumberLeaf(mytree):
    numberLeaf = 0
    firstTree = list(mytree)[0]
    secondDict = mytree[firstTree]
    for key in secondDict.keys():
        if type(secondDict[key]).__name__ == 'dict':
            numberLeaf += getNumberLeaf(secondDict)
        else:
            numberLeaf += 1
    return numberLeaf


getNumberLeaf({'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}})
